package com.ninong.ker.admin.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ninong.ker.admin.domain.DicDetail;
import com.ninong.ker.admin.mapper.DicDetailMapper;
import com.ninong.ker.admin.service.DicDetailService;
import com.ninong.ker.admin.service.dto.DicDetailDTO;
import com.ninong.ker.common.exception.CmdException;
import com.ninong.ker.common.tools.Constants;
import com.ninong.ker.common.tools.enums.BusinessEnum;

/**
 * 
 * @author cmd
 *
 */
@Service
public class DicDetailServiceImpl extends ServiceImpl<DicDetailMapper, DicDetail> implements DicDetailService {

	@Override
	public List<DicDetail> listPage(DicDetailDTO dto) {
		QueryWrapper<DicDetail> query = new QueryWrapper<>();
		query.eq("dic_id", dto.getDicId());
		return baseMapper.selectList(query);
	}

	@Override
	public void delete(String[] ids) {
		baseMapper.deleteBatchIds(Arrays.asList(ids));
	}

	@Override
	public void create(DicDetail entity) {
		Integer count = null;
		if (StringUtils.isEmpty(entity.getId())) {
			count = baseMapper.selectCount(
					new QueryWrapper<DicDetail>().eq("dic_id", entity.getDicId()).eq("code", entity.getCode()));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.DIC_CODE_REPEAT);
			}
		} else {
			count = baseMapper.selectCount(new QueryWrapper<DicDetail>().ne("id", entity.getId())
					.eq("dic_id", entity.getDicId()).eq("code", entity.getCode()));
			if (count > Constants.ZERO) {
				throw new CmdException(BusinessEnum.DIC_CODE_REPEAT);
			}

		}
		saveOrUpdate(entity);
	}

	@Override
	public DicDetail get(String id) {
		DicDetail entity = baseMapper.selectById(id);
		if (ObjectUtils.isEmpty(entity)) {
			entity = new DicDetail();
		}
		return entity;
	}

}
